package com.zgkj.Zip.tools;

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;

public class UploadFile {
	public void upload(String localFile){
        File file = new File(localFile);
        PostMethod filePost = new PostMethod("http://localhost:8888/lf/api/entra_file");
        //PostMethod filePost = new PostMethod("http://localhost:8888/MyTestWeb//servlet/HelloWorld");
        HttpClient client = new HttpClient();

        try {
            // 通过以下方法可以模拟页面参数提交
            filePost.setParameter("userName", "kcf");
            filePost.setParameter("passwd", "123");
            Part[] parts = { new FilePart(file.getName(), file) };
            filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
            client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            int status = client.executeMethod(filePost);
            System.out.println(status);
            if (status == HttpStatus.SC_OK) {
                System.out.println("上传成功");
            } else {
                System.out.println("上传失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            filePost.releaseConnection();
        }
    }

	public void upload1(String localFile){

		  String str="http://localhost:8888/lf/api/entra_file";
	        String filePath="d:/GA000125257394_2001031.xml";
	        String fileName="GA000125257394_2001031.xml";
	        try {
	            URL url=new URL(str);
	            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
	            connection.setDoInput(true);
	            connection.setDoOutput(true);
	            connection.setRequestMethod("POST");
	            connection.addRequestProperty("FileName", fileName);
	            connection.setRequestProperty("content-type", "text/html");
	            BufferedOutputStream  out=new BufferedOutputStream(connection.getOutputStream());

	            //读取文件上传到服务器
	            File file=new File(filePath);
	            FileInputStream fileInputStream=new FileInputStream(file);
	            byte[]bytes=new byte[1024];
	            int  numReadByte=0;
	            while((numReadByte=fileInputStream.read(bytes,0,1024))>0)
	            {
	                out.write(bytes, 0, numReadByte);
	            }

	            out.flush();
	            fileInputStream.close();
	            //读取URLConnection的响应
	            DataInputStream in=new DataInputStream(connection.getInputStream());
	        } catch (Exception e) {
	            e.printStackTrace();
	        }

	}

	public static void main(String[] args) {
		UploadFile UploadFile = new UploadFile();
		UploadFile.upload("D:/550161910/cnnct-core.jar");
	}
}
